// components/manual/manual.js
import {throttle} from "../../utils/util"
import Toast from '@vant/weapp/toast/toast';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
         
  },

  /**
   * 组件的初始数据
   */
  data: {
    dataList:[],
    shopData:[],
    shopInfo:{
      categoryFirst:"",
      categorySecond:"",
      page:1,
      size:10
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
   async getover(e){
    this.setData({
      ['shopInfo.categoryFirst']:e
    })
    let res = await getApp().api.findCategory({categoryFirst:e})
    console.log(res.data)
    if(res.code!=200)return
      this.setData({
       dataList:res.data
     })
     this.getShops()
  },
  //获取列表
  handleChildData(e){
      // console.log(e.detail)
      if(e.detail){
        this.setData({
          ['shopInfo.categorySecond']:this.data.dataList[e.detail].id,
          ['shopInfo.page']:1
        })
      }else{
        this.setData({
          ['shopInfo.categorySecond']:"",
          ['shopInfo.page']:1
        })
      }
      this.getShops()
  },
  async getShops(){
    // console.log(this.data.shopInfo.page)
    let res = await getApp().api.getShoplist(this.data.shopInfo)
    // console.log(res.data)
     if(res.code!=200)return
     res.data.data.forEach(element => {
      element.picUrls= JSON.parse(element.picUrls.replace(/\'/g,"\""))
      element.yyTimes?JSON.parse(element.yyTimes):[]
     })
     if(this.data.shopInfo.page==1){
      this.setData({
        shopData:res.data.data
      })
     }else{
      // console.log(res.data.data)
      this.setData({
        shopData:this.data.shopData.concat(res.data.data)
      })
      if(res.data.data.length<=0){
        this.setData({
          ['shopInfo.page']:this.data.shopInfo.page-1
        })
      }
     }
    },
   toshopDetail(e){
     wx.navigateTo({
       url: '/packageA/pages/shopDetail/index?item='+encodeURIComponent(JSON.stringify(e.currentTarget.dataset.item)),
     })
   },
        //收藏
  async collectTo(e){
    // console.log(e.currentTarget.id)
    let i =  e.currentTarget.id
   if(this.data.shopData[i].collect){//取消收藏
        let result = await getApp().api.clearCollect([this.data.shopData[i].id])
        // console.log(result)
        if(result.code!=200)return  Toast.fail(result.msg);
          Toast('已取消收藏');
          this.setData({
            [`shopData[${i}].collect`]:false,
          })   
   }else{ //收藏
    let data = {
      spuId:this.data.shopData[i].id,
      addPrice:this.data.shopData[i].paymentPrice,
      quantity:1,
      spuName:this.data.shopData[i].name,
      size:''
     }
    // console.log(data)
     let res= await getApp().api.postCollect(data)
      // console.log(res)
     if(res.code!=200)return Toast.fail(res.msg);
    this.setData({
      [`shopData[${i}].collect`]:true,
    })
    //修改状态
    Toast.success('收藏成功');
   } 

},
   //商品滑动到底部
   tolower(e){throttle(()=>{
       this.setData({
        ['shopInfo.page']:this.data.shopInfo.page+1
       })
       this.getShops()
    })},
  },
})
